package drds.common.db.xml_mapping.executor.statement;


import drds.common.db.xml_mapping.executor.Executor;
import drds.common.db.xml_mapping.executor.ExecutorException;
import drds.common.db.xml_mapping.executor.parameter.ParameterHandler;
import drds.common.db.xml_mapping.mapping.BindSql;
import drds.common.db.xml_mapping.mapping.Mapper;
import drds.common.db.xml_mapping.session.ResultHandler;
import drds.common.db.xml_mapping.session.RowBounds;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;


public class RoutingStatementHandler implements StatementHandler {

  private final StatementHandler delegate;

  public RoutingStatementHandler(Executor executor, Mapper MappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BindSql bindSql) {

    switch (MappedStatement.getStatementType()) {
      case statement:
        delegate = new SimpleStatementHandler(executor, MappedStatement, parameter, rowBounds, resultHandler, bindSql);
        break;
      case prepared_statement:
        delegate = new PreparedStatementHandler(executor, MappedStatement, parameter, rowBounds, resultHandler, bindSql);
        break;
      default:
        throw new ExecutorException("Unknown statement parameter_and_result_value_type: " + MappedStatement.getStatementType());
    }

  }

  @Override
  public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
    return delegate.prepare(connection, transactionTimeout);
  }

  @Override
  public void parameterize(Statement statement) throws SQLException {
    delegate.parameterize(statement);
  }

  @Override
  public void batch(Statement statement) throws SQLException {
    delegate.batch(statement);
  }

  @Override
  public int update(Statement statement) throws SQLException {
    return delegate.update(statement);
  }

  @Override
  public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
    return delegate.query(statement, resultHandler);
  }


  @Override
  public BindSql getBindSql() {
    return delegate.getBindSql();
  }

  @Override
  public ParameterHandler getParameterHandler() {
    return delegate.getParameterHandler();
  }
}
